/**
 * @date 2006-1-6 19:23:09
 *
 */
package org.nanhill.commons.html;

import static org.nanhill.commons.html.XhtmlConstants.BORDER;
import static org.nanhill.commons.html.XhtmlConstants.CELLPADDING;
import static org.nanhill.commons.html.XhtmlConstants.CELLSPACING;
import static org.nanhill.commons.html.XhtmlConstants.COLSPAN;
import static org.nanhill.commons.html.XhtmlConstants.ROWSPAN;
import static org.nanhill.commons.html.XhtmlConstants.TABLE;
import static org.nanhill.commons.html.XhtmlConstants.TD;
import static org.nanhill.commons.html.XhtmlConstants.TR;

import java.io.FileOutputStream;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * @author alin
 * 
 */
public class Dom4jXhtmlTableTidy {
	// private Element desFather;

	// private Element srcFather;

	// private Document desDoc;

	private void copyAttr(Element src, Element des, String attrName) {
		String attrValue = src.attributeValue(attrName);
		if (null != attrValue) {
			des.addAttribute(attrName, attrValue);
		}
	}

	private void copyText(Element src, Element des) {
		des.addText(src.getText());
	}

	private void buildTable(Element srcTable, Element desTable) {
		this.copyAttr(srcTable, desTable, BORDER);
		this.copyAttr(srcTable, desTable, CELLPADDING);
		this.copyAttr(srcTable, desTable, CELLSPACING);
		for (Iterator itt = srcTable.elementIterator(); itt.hasNext();) {
			Element srcTr = (Element) itt.next();
			if (srcTr.getName().equals(TR)) {
				this.buildTr(srcTr, desTable.addElement(TR));
			}
		}
	}

	private void buildTr(Element srcTr, Element desTr) {
		for (Iterator itt = srcTr.elementIterator(); itt.hasNext();) {
			Element srcTd = (Element) itt.next();
			if (srcTd.getName().equals(TD)) {
				this.buildTd(srcTd, desTr.addElement(TD));
			}
		}
	}

	private void buildTd(Element srcTd, Element desTd) {
		this.copyAttr(srcTd, desTd, ROWSPAN);
		this.copyAttr(srcTd, desTd, COLSPAN);
		this.copyText(srcTd, desTd);
	}

	public Document tidy(Document src) {
		Document desDoc = DocumentHelper.createDocument();
		Element root = src.getRootElement();
		if (root.getName().equals(TABLE)) {
			this.buildTable(root, desDoc.addElement(TABLE));
		}
		return desDoc;
	}

	public void tidy(String srcFilename, String... desFilenames) {
		try {
			Document srcDoc = new SAXReader().read(srcFilename);
			System.out.println(srcDoc.asXML());
			for (int i = 0; i < desFilenames.length; i++) {
				Document desDoc = tidy(srcDoc);
				XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(
						desFilenames[i]));
				xmlWriter.write(desDoc);
				xmlWriter.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		int argLen = args.length;
		if (argLen >= 2) {
			String[] dess = new String[argLen - 1];
			System.arraycopy(args, 1, dess, 0, argLen - 1);
			new Dom4jXhtmlTableTidy().tidy(args[0], dess);
		} else {
			System.out.println("You must input more than 2 arguments.");
		}
	}
}
